/* 
 * File:   BackupRobot.cpp
 * Author: sysel
 * 
 * Created on April 24, 2011, 9:17 PM
 */


#include "BackupRobot.h"

BackupRobot* BackupRobot::theSingleInstance = 0;

BackupRobot* BackupRobot::Instance() {
    if(theSingleInstance == 0)
        theSingleInstance = new BackupRobot;

    return theSingleInstance;
}

BackupRobot::BackupRobot() {
}

BackupRobot::~BackupRobot() {
}

Uid BackupRobot::getUniqueID(){
    if(uidVector == 0)
        uidVector = new std::vector<Uid>;

    Uid newUid;
    bool finded;
    do{
        newUid = random() % MAX_UID;

        finded = false;
        for(std::vector<Uid>::const_iterator i = uidVector->begin(); i != uidVector->end(); i++ ) {
            if(newUid == *i) {
                finded = true;
                break;
            }
        }
    }while(finded);

    return newUid;
}


void BackupRobot::logData(Message mes) {
    char *fname;
	const char* dirname = "/usr/share/gpsmond";
    sprintf(fname, "%s/%d", dirname, mes.uid);
    FILE *file = fopen(fname, "a+");
	if(0 == file) {
		char *bufmes;		
		sprintf(bufmes, "Can not open file %s!", fname);
		throw bufmes;
	}
    //sprintf(fname, "%d_temp", uid);
    //FILE *tmp = fopen(fname, "w");

    //char buf[16];
    //buf[0] = mes.latt;
    //buf[8] = mes.lont;

    //write(file, buf, 16);
	fwrite(&mes, sizeof(Message), sizeof(Message), file);

	fclose(file);
}

